package com.code;

import com.code.lin.Base64Utils;
import com.code.lin.LicenseGenerator;
import com.code.lin.RSAUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;

@SpringBootApplication
@RequestMapping("/")
public class Application  {

	@RequestMapping("/")
	public String index() {
		return "index";
	}


	@RequestMapping(value = "/getCode",method = RequestMethod.GET)
	@ResponseBody
	public String getCode(String jqm,String gqsj) {
		String res ="";
		try {
			String code = AuthorizationUtils.authCodeVal(jqm);

			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			String licensestatic = "serial="+code+";timeEnd="+sf.parse(gqsj).getTime();

			//System.out.println("原文字：\r\n" + licensestatic);
			byte[] data = licensestatic.getBytes();
			byte[] encodedData = RSAUtils.encryptByPrivateKey(data, LicenseGenerator.privateKey);
			//System.out.println("加密后：\r\n" + new String(encodedData)); //加密后乱码是正常的

			String path = "d:/license.dat";

			//Base64Utils.byteArrayToFile(encodedData, FileUtil.getBasePath()+ File.separator+"license.dat");
			Base64Utils.byteArrayToFile(encodedData, path);
			res = "证书生成成功，文件位置:"+path;
		} catch (Exception e) {
			e.printStackTrace();
			res = e.getMessage();
		}
		return res;
	}


	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}
